const conn = require("../../libs/DB/Conn");
const _ = require("underscore");
const {createSql, projectDeviceDataUniq,trendTimeRange} = require("./dataAnalysisUtil");

module.exports = async function(params){
    let time = trendTimeRange();
    let sql = createSql(params.type,params.userDomainId,"day",time.range,{type:"project"},null,null);

    let result = await conn(sql.sql);
    result = projectDeviceDataUniq(result);
    let projectGroupList = _.groupBy(result,"projectId");
    let dataList = [];
    for(const projectId in projectGroupList){
        let projectList = projectGroupList[projectId];
        let dateDataGroup = _.groupBy(projectList,"dataTime");
        let projectDataList = [];
        for(const date of time.dateList){
            projectDataList.push(dateDataGroup[date] ? dateDataGroup[date][0].consumption : "-");
        }
        dataList.push({
            name:projectList[0].projectName,
            data:projectDataList
        })
    }
    return{success:true,dataList:dataList,xAxis:time.dateList};
}